package cn.fl.fgi.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.fgi.api.IFgiUpDetailApiService;
import cn.fl.fgi.api.condition.FgiUpDetailQueryCondition;
import cn.fl.fgi.api.dto.FgiUpDetailDTO;
import cn.fl.fgi.transform.FgiUpDetailVOTransform;
import cn.fl.fgi.vo.FgiUpDetailVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 文件详情信息 控制层
 */
@Controller
@RequestMapping("/fgi/updetail")
public class FgiUpDetailController extends BaseController {


    @Autowired
    private IFgiUpDetailApiService fgiUpDetailApiService ;

    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService ;
    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("fgi/fgiUpDetailListPage");
        String id = request.getParameter("fgiUpDataId");
        List<KeyValueVO> payNoStatusList = sysDictionaryApiService.getKvList("11000","0000","FGI0001");

        view.addObject("fgiUpDataId",id);
        view.addObject("payNoStatusList",payNoStatusList);
        return view;
    }
    /**
     * 到列表页面
     */
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initList(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("fgiUpDataId");
        String currentPage = request.getParameter("page");
        String page = request.getParameter("pagesize");
        String payNoStatusCd = request.getParameter("payNoStatusCd");
        String payStatusId = request.getParameter("payStatusId");
        String prjOrgNm = request.getParameter("prjOrgNm");
        Integer pageNum = 1 ;
        Integer pageSize = 10 ;
        if(StringUtils.isNotEmpty(currentPage)){
            pageNum = Integer.valueOf(currentPage);
        }
        if(StringUtils.isNotEmpty(page)){
            pageSize = Integer.valueOf(page);
        }
        FgiUpDetailQueryCondition condition = new FgiUpDetailQueryCondition();
        condition.setFgiUpDataId(Long.valueOf(id));
        if(StringUtils.isNotEmpty(payNoStatusCd)){
            condition.setPayStatusId(Integer.valueOf(payStatusId));
        }
        if(StringUtils.isNotEmpty(prjOrgNm)){
            condition.setOrgNm(prjOrgNm);
        }
        String s = fgiUpDetailApiService.searchListPage(condition,pageNum,pageSize);
        Integer total = 1 ;
        List<FgiUpDetailDTO> dtoList = null ;
        ListPageVO<FgiUpDetailVO> listPageVO = null ;
        if(StringUtils.isNotEmpty(s)) {
            JSONObject obj = JSONObject.parseObject(s);
            Object o = obj.get("total");
            if(o != null ){
                total = Integer.valueOf(o.toString());
            }
            Object data = obj.get("dataList");
            if(data != null){
                dtoList = JSON.parseArray(data.toString(),FgiUpDetailDTO.class);
                if(CollectionUtils.isNotEmpty(dtoList)){
                    for (FgiUpDetailDTO dto:dtoList) {
                       // dto.setOverdueTm(new Date());
                    }
                }
            }
        }
        listPageVO = new ListPageVO<FgiUpDetailVO>(FgiUpDetailVOTransform.toVOList(dtoList),total);
        return listPageVO;
    }
}
